## 

rm(list = ls())

library(tidyverse)
library(readxl)
library(janitor)
library(cowplot)

## Upper half of the plot gets created first

## Load Manifesto Data

manif <- read_xlsx("data/manifesto_2019.xlsx") %>%
    filter(countryname == "Germany") %>%
    dplyr::select(date, partyname, rile) %>%
    mutate(date = substr(date, 1, 4)) %>%
    mutate(
        partyname =
            dplyr::recode(partyname,
                `Alliance‘90/Greens` = "greens",
                `Alternative for Germany` = "afd",
                `Christian Democratic Union/Christian Social Union` = "cdu_csu",
                `Free Democratic Party` = "fdp",
                `Greens/Alliance'90` = "greens",
                `Party of Democratic Socialism` = "left",
                `Social Democratic Party of Germany` = "spd",
                `The Greens` = "greens",
                `The Left` = "left",
                `The Left. Party of Democratic Socialism` = "left"
            )
    ) %>%
    filter(!partyname == "Pirates") %>%
    mutate(date = as.numeric(date)) %>%
    dplyr::rename(
        party = partyname,
        year = date
    )

## Load Countrywide election results

btw <- read_xlsx("data/federal_election_agg_results.xlsx", sheet = 2) %>%
    mutate_all(as.numeric) %>%
    rename(year = 1) %>%
    pivot_longer(
        cols = -1,
        names_to = "party",
        values_to = "voteshare"
    ) %>%
    left_join(manif) %>%
    filter(!is.na(rile))

## Vote share divide by 100

btw <- btw %>%
    mutate(voteshare = voteshare / 100)

## Small party vote share over time

btw <- btw %>%
    mutate(small_party = ifelse(party %in% c("cdu_csu", "spd"),
        0, 1
    ))

## Vote share of big and small parties

bigsmall <- btw %>%
    group_by(small_party, 
    year) %>%
    summarize(voteshare = sum(voteshare)) %>%
    mutate(ifelse(small_party == 0,
        voteshare, 1 - voteshare
    ))

## ##

p1 <- ggplot(
    subset(bigsmall, year >= 1980),
    aes(
        x = year, y = voteshare,
        group = factor(small_party),
        linetype = factor(small_party)
    )
) +
    geom_line() +
    geom_point(shape = 21, fill = "black") +
    theme_bw() +
    labs(
        x = "",
        y = "Cumulative vote share in\nGerman federal elections"
    ) +
    scale_y_continuous(breaks = seq(.1, .9, .1)) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        axis.title.x = element_blank()
    ) +
    xlim(1980, 2020) +
    scale_linetype_manual(
        values = c("dashed", "solid"),
        name = "",
        labels = c(
            "Large centrist\nparties (CDU/CSU & SPD)",
            "Other parties"
        )
    )

p1

## Second half of the figure ##

rm(list = lm[!lm() == "p1"])

## Load Data for the second half 

files <- list.files("data",
    pattern = "circulation",
    full.names = T
)

## Process

data <- lapply(files, function(path) {
    read_xlsx(path, sheet = 2, skip = 3) %>%
        mutate(country = path) %>%
        clean_names() %>%
        rename(
            year = 1,
            value = 2
        ) %>%
        mutate(
            country = case_when(
                str_detect(country, "deu") ~ "Germany",
                str_detect(country, "usa") ~ "USA"
            )
        ) %>%
        mutate(
            variable = case_when(
                str_detect(country, "Germany") ~ "Circulation of daily newspapers (in million copies)",
                str_detect(country, "USA") ~ "Circulation of daily newspapers (in million copies)"
            )
        )
}) %>%
    reduce(rbind) %>%
    mutate(
        year2 = str_extract_all(year, "[[:digit:]]+"),
        year2 = as.numeric(year2)
    ) %>%
    filter(!is.na(year2))


## Transform year variable for US Data

data <- data %>%
    mutate(
        year2 = case_when(
            year2 > 70 & year2 < 100 ~ paste("19", year2, sep = ""),
            year2 < 10 & year2 < 100 ~ paste("200", year2, sep = ""),
            year2 >= 10 & year2 < 20 ~ paste("20", year2, sep = ""),
            TRUE ~ as.character(year2)
        )
    ) %>%
    mutate(year2 = as.numeric(year2))


## Transform US/Norway data to circulation in millions
## (was measures in thousands in original data source)

data <- data %>%
    mutate(value = ifelse(country %in% c("USA", "Norway", "Sweden"),
        value / 1000, value
    )) %>%
    filter(country %in% c("USA", "Germany"))


## Scale by population

pop <- read_xlsx("data/country_population_data.xlsx") %>%
    mutate(
        pop = as.numeric(pop),
        pop = pop / 1000,
        pop = ifelse(country == "Germany" &
            year >= 1991, pop * 10, pop)
    ) %>%
    rename(year2 = year) %>%
    mutate(year2 = as.numeric(year2))



## Merge

data <- data %>%
    left_join(pop) %>%
    mutate(value_scaled = value / pop) %>%
    filter(year2 >= 1985)

## Plot

p2 <- ggplot(data, aes(
    x = year2, y = value_scaled,
    group = country,
    color = country
)) +
    geom_line(aes(color = country)) +
    geom_point(shape = 21, aes(fill = country)) +
    theme_bw() +
    scale_color_manual(
        values = c("Black", "grey55"),
        name = ""
    ) +
    scale_fill_manual(
        values = c("Black", "grey55"),
        name = ""
    ) +
    labs(
        x = "",
        y = "Circulation of daily newspapers\n(per capita)"
    ) +
    scale_x_continuous(breaks = seq(1985, 2020, 10), limits = c(1985, 2020)) +
    theme(legend.position = "bottom") +
    theme(axis.title.x = element_blank())
p2

## Combine via cowplot

cowplot::plot_grid(p1, p2 +
                     xlim(1980, 2020) +
                     theme(legend.position = 'bottom'),
                   align = 'hv', nrow = 2)


